// handlers/HTMLHandler.js
const BaseHandler = require('./BaseHandler');
const fs = require('fs');
const path = require('path');

class HTMLHandler extends BaseHandler {
    async handle(req, res, filePath) {
        try {
            if (!await this.fileExists(filePath)) {
                return this.sendError(res, 404, 'HTML file not found');
            }

            // 设置正确的Content-Type
            res.setHeader('Content-Type', 'text/html; charset=utf-8');
            res.setHeader('Cache-Control', 'public, max-age=300');

            // 使用流式传输提高大文件处理性能
            const stream = fs.createReadStream(filePath, { encoding: 'utf8' });

            stream.on('error', (error) => {
                console.error('Error reading HTML file:', error);
                this.sendError(res, 500, 'Error reading HTML file');
            });

            stream.pipe(res);

        } catch (error) {
            console.error('Error processing HTML file:', error);
            this.sendError(res, 500, 'Failed to process HTML file');
        }
    }
}

module.exports = HTMLHandler;